home *** CD-ROM | disk | FTP | other *** search
- /******************************************************************************
- *
- * NSSDC/CDF Build screen-arrays for CDFbrowse.
- *
- * Version 2.0, 2-Mar-92, ST Systems (STX)
- *
- * Modification history:
- *
- * V1.0 24-Jan-91, D Grogan Original version (for CDF V2.0).
- * H Leckner
- * V1.1 30-Jan-91, J Love Shortened text from CDFerror so as not to
- * overwrite arrays.
- * V1.2 31-Jan-91, J Love Check for rcode (CDFstatus) < CDF_WARN rather
- * than rcode != CDF_OK to determine if an error
- * has occurred. Fixed problem with searching
- * for TITLE attribute (pad to 8 characters).
- * Fixed problem with attribute entry lines being
- * too long.
- * V1.3 8-Feb-91, J Love Limit length of line used to display variable
- * and attribute values.
- * V1.4 21-Mar-91, J Love Added support for Silicon Graphics (MIPSEB)
- * and removed reference to "strstr".
- * V1.5 26-Mar-91, J Love Added definition for toupper if SunOS 4.0.3.
- *
- * V1.6 6-Jun-91, S Sudarsan Now uses the internal interface for getting
- * the header information. Also displays format.
- * Added increment to version display.
- * V1.7 19-Jun-91, J Love CDF_EPOCH added as a data type. Fixed problem
- * with FORMAT attribute. Changed default format
- * for floating point values if FORMAT attribute
- * not present.
- * V1.8 3-Aug-91, J Love TRUE/FALSE. Changed for Cray/UNICOS port.
- * Display CDF_UINT1 & CDF_UCHAR as unsigned.
- * Use 'CDFlib'. Fixed problem with truncation
- * message being too long. Reformatted window
- * displayed on CDF open error.
- * V2.0 2-Mar-92, J Love Modified for IBM-PC port (and IBM-RS6000).
- * H Leckner CDF V2.2.
- *
- ******************************************************************************/
-
- /*
- Inquiry module for CDFbrowse that builds screen-arrays to WFL specs.
-
- This module is subordinate to CDFbrowse main program.
- This module never calls WFL, but uses wfl.h constants.
- This module does call CDF and acts as interfacer to CDF for the
- CDFbrowse main module, which never calls CDF directly.
-
- There are currently no dependencies on the toolkit per se.
- Some toolkit function are included in different form.
- Requires:
- wfl.h --standard WFL prototype header
- epochu.obj
- epochu.h --epochu module and header as
- revised for CDF v2 release.
- */
-
- #if 0
- static int dummyFn() /* debugging use only */
- { return;}
- #endif
-
- #include "wfl.h"
-
- #include <stdlib.h>
- #include <time.h>
- #include <stdio.h>
- #include <ctype.h>
- #include <string.h>
-
- #include "cdfdist.h"
- #include "cdfbrow.h"
-
- /* some things that need a limiting value do not have an official one
- in the CDF spec, so here one is decreed for CDFbrowse.
- */
- #define BIG_ENOUGH 2048 /* I hope it's big enough */
- #define BROWSE_MAX_ATTRS BIG_ENOUGH
- /* If the CDF has > BROWSE_MAX_ATTRS attributes the program blows up,
- no protection.
- BROWSE_MAX_ATTRS can be replaced by using the dynaptr.h macros
- in the same way that they are used in CDFbrowse, to allocate just
- enough attribute structs according their number. To do this, you
- must also create a typedef struct ATTR_DEF ATTR_DEF_STRUCT so that
- the macros can expand on the structure type. That is, 1-token
- expansion is ok; 2-token ("struct ATTR_DEF") is not. -- dg.
- */
-
- /*This union structure supercedes the previous ALLTYPES
- union found in the toolkit and in WFL.
- */
- union DATATYPE_UNION { /* CDF types */
- Schar byted[BIG_ENOUGH]; /* _BYTE, (_CHAR), _INT1 */
- Uchar uchard[BIG_ENOUGH]; /* (_UCHAR), _UINT1 */
- short int2; /* _INT2 */
- unsigned short uint2; /* _UINT2 */
- long int4; /* _INT4 */
- unsigned long uint4; /* _UINT4 */
- float real4; /* _REAL4, _FLOAT */
- double real8; /* _REAL8, _DOUBLE, _EPOCH */
- };
-
- #define pf printf
- #define fpf fprintf
- #define spf sprintf
-
- #define varncTF(string,variance,n) \
- (variance) ? strncat(string, "True ", n) : strncat(string, "false", n)
-
- #define FIRST_ELEM -1 /* Must be negative. look below */
- #define EPOCH_TYPE -777 /* not a CDF type ! An extension */
- /* See writemTypedValue() */
- static struct CDF_DEF cdfHeaderStruct;
- static struct ATTR_DEF *attrList[BROWSE_MAX_ATTRS]; /* See warning above */
- static struct VAR_DEF *varList[CDF_MAX_VARS];
-
- static struct CDF_DEF *cdfh;
- static struct ATTR_DEF *attr;
- static struct VAR_DEF *var;
-
- /*static struct EPOCH epoch;*/
-
- char errorText[CDF_ERRTEXT_LEN+1]; /* V1.1 */
-
- long increment; /* V1.6 */
-
- char *dataTypeName( dataType)
- long dataType;
- {
- /* return the name-string of the CDF dataType, minus the "CDF" prefix */
- switch( dataType) {
- case CDF_BYTE: return( "_BYTE ");
- case CDF_CHAR: return( "_CHAR ");
- case CDF_INT1: return( "_INT1 ");
- case CDF_UCHAR: return( "_UCHAR ");
- case CDF_UINT1: return( "_UINT1 ");
- case CDF_INT2: return( "_INT2 ");
- case CDF_UINT2: return( "_UINT2 ");
- case CDF_INT4: return( "_INT4 ");
- case CDF_UINT4: return( "_UINT4 ");
- case CDF_REAL4: return( "_REAL4 ");
- case CDF_FLOAT: return( "_FLOAT ");
- case CDF_REAL8: return( "_REAL8 ");
- case CDF_DOUBLE: return( "_DOUBLE");
- case CDF_EPOCH: return( "_EPOCH ");
- default: return( "unknown");
- }
- } /* end dataTypeName */
-
-
- long dataTypeSizeof( dataType)
- long dataType;
- {
- /* return the length of a CDF dataType */
- switch( dataType) {
- case CDF_BYTE:
- case CDF_CHAR:
- case CDF_INT1:
- case CDF_UCHAR:
- case CDF_UINT1: return 1;
-
- case CDF_INT2:
- case CDF_UINT2: return 2;
-
- case CDF_INT4:
- case CDF_UINT4:
- case CDF_REAL4:
- case CDF_FLOAT: return 4;
-
- case CDF_EPOCH:
- case CDF_REAL8:
- case CDF_DOUBLE: return 8;
-
- default: return 0;
- }
- } /* end dataTypeSizeof */
-
-
- long openCDFtoBrowse (dir, CDFname, out, values_per_record)
- char *dir;
- char *CDFname;
- char *out[];
- long *values_per_record;
- {
- /* Open the CDF; read CDF header, attribute, and variable structs;
- Write header text to *out[curRow++,...] page.
- */
- CDFstatus rcode;
- int curRow, j, attrNum, dataType, nElements;
- char msg[CDF_ERRTEXT_LEN+1]; /* V1.1 */
- char tempString[BIG_ENOUGH];
-
- cdfh = &cdfHeaderStruct;
- curRow = 0;
- spf (out[curRow++]," CDF Name: %s", CDFname );
-
- strcpy (cdfh->CDFname, dir);
- AppendToDir (cdfh->CDFname, CDFname);
-
- rcode = CDFopen( cdfh->CDFname, &cdfh->id);
-
- if (rcode < CDF_WARN) { /* EARLY RETURN !!! */ /* V1.2 */
- char *ptr;
- spf (out[curRow++], " Error --- Cannot open CDF. ");
- CDFerror (rcode, errorText); /*V1.1*/
- if ((ptr = strchr(errorText,':')) != NULL) *ptr = NUL; /*V1.1*/
- spf (out[curRow++], " CDF status code: %s ",errorText); /*V1.1*/
- return (0); /* 0 means the CDF is not open .*/
- }
-
- /* Load CDF header struct */
-
- rcode = CDFlib (SELECT_, CDF_, cdfh->id,
- GET_, CDF_NUMDIMS_, &cdfh->nDims,
- CDF_DIMSIZES_, cdfh->dimSizes,
- CDF_ENCODING_, &cdfh->encoding,
- CDF_MAJORITY_, &cdfh->majority,
- CDF_MAXREC_, &cdfh->maxRec,
- CDF_NUMVARS_, &cdfh->nVars,
- CDF_NUMATTRS_, &cdfh->nAttrs,
- CDF_FORMAT_, &cdfh->format,
- NULL_); /* V1.6 */
-
- if (rcode < CDF_WARN) { /* EARLY RETURN !!! */ /* V1.2 */
- spf (out[curRow++],
- " Error -- Cannot get attributes from CDF %s", CDFname);
- spf (out[curRow++],
- " on the current directory.");
-
- /*** CDFerror( rcode, out[curRow++]); ***/ /* V1.1 */
- CDFerror (rcode, errorText); /* V1.1 */
- *strchr(errorText,':') = NUL; /* V1.1 */
- strcpy (out[curRow++], errorText); /* V1.1 */
-
- return(0); /* 0 means the CDF is not open */
- }
-
- *values_per_record = 1;
- for(j=0; j<cdfh->nDims; j++)
- *values_per_record = *values_per_record * cdfh->dimSizes[j];
-
- rcode = CDFdoc( cdfh->id, &cdfh->version, &cdfh->release,
- &cdfh->docText[0]);
- if (rcode < CDF_WARN) { /* EARLY RETURN !!! */ /* V1.2 */
-
- /*** CDFerror( rcode, out[curRow++]); ***/ /* V1.1 */
- CDFerror (rcode, errorText); /* V1.1 */
- *strchr(errorText,':') = NUL; /* V1.1 */
- strcpy (out[curRow++], errorText); /* V1.1 */
-
- return(0); /* 0 means the CDF is not open */
- }
- /* Fill the header text page */
- spf (out[curRow++]," Dimensions: %2ld ", cdfh->nDims);
- /* don't increment curRow yet */
- spf (out[curRow], " sizes: [");
- for (j=0; j<cdfh->nDims; j++) {
- spf (tempString,"%ld", cdfh->dimSizes[j]);
- strcat (out[curRow], tempString);
- if ( j+1 != cdfh->nDims) strcat (out[curRow],",");
- }
- /* resume incrementing curRow */
- strcat (out[curRow++],"]");
- spf (out[curRow++]," Records:%6ld", cdfh->maxRec+1);
- spf (out[curRow++]," Attributes: %2ld", cdfh->nAttrs);
- spf (out[curRow++]," Variables: %2ld", cdfh->nVars);
- switch( cdfh->encoding) {
- case VAX_ENCODING:
- spf (out[curRow++]," encoding: VAX");
- break;
- case SUN_ENCODING:
- spf (out[curRow++]," encoding: Sun");
- break;
- case MIPSEL_ENCODING:
- spf (out[curRow++]," encoding: MIPSEL");
- break;
- case MIPSEB_ENCODING: /* V1.4 */
- spf (out[curRow++]," encoding: MIPSEB");
- break;
- case IBMRS_ENCODING:
- spf (out[curRow++]," encoding: IBMRS");
- break;
- case IBMPC_ENCODING:
- spf (out[curRow++]," encoding: IBMPC");
- break;
- case HP_ENCODING:
- spf (out[curRow++]," encoding: HP");
- break;
- case NETWORK_ENCODING:
- spf (out[curRow++]," encoding: Network");
- break;
- default:
- spf (out[curRow++]," encoding: ? Unknown");
- break;
- }
- switch( cdfh->majority) {
- case ROW_MAJOR:
- spf (out[curRow++]," majority: Row");
- break;
- case COL_MAJOR:
- spf (out[curRow++]," majority: Column");
- break;
- default:
- spf (out[curRow++]," majority: ? Unknown");
- break;
- }
-
- switch( cdfh->format) { /* V1.6 */
- case SINGLE_FILE:
- spf (out[curRow++]," Format: Single");
- break;
- case MULTI_FILE:
- spf (out[curRow++]," Format: Multi");
- break;
- default:
- spf (out[curRow++]," Format: ? Unknown");
- break;
- }
-
- rcode = CDFlib (SELECT_, CDF_, cdfh->id,
- GET_, CDF_INCREMENT_, &increment,
- NULL_); /* V1.6 */
-
- spf (out[curRow++]," version: %ld.%ld.%ld",
- cdfh->version, cdfh->release, increment); /* V1.6 */
-
- /* allocate space for attribute and variable structs.
- REPLACE with maocros in dynaptr.h for best practice. --dg.
- These spaces get freed in cdfCloseQuit().
- **/
- for (j=0; j<cdfh->nAttrs; j++)
- {
- attrList[j] = (struct ATTR_DEF *) malloc (sizeof(struct ATTR_DEF));
- if(attrList[j] == NULL)
- {
- printf("BAD MALLOC, attr_num %d",j);
- return(BAD_MALLOC);
- }
- }
- for (j=0; j<cdfh->nVars; j++)
- {
- varList[j] = (struct VAR_DEF *) malloc (sizeof(struct VAR_DEF));
- if(varList[j] == NULL)
- {
- printf("BAD MALLOC, var_num %d",j);
- return(BAD_MALLOC);
- }
- }
- /*----------Load attributes structs **/
-
- for (j=0; j<cdfh->nAttrs; j++) {
- attr = attrList[j];
- attr->attrNum = j;
- rcode = CDFattrInquire( cdfh->id,
- attr->attrNum,
- &attr->name[0],
- &attr->scope,
- &attr->maxEntry);
- if (rcode < CDF_WARN) { /* V1.2 */
- CDFerror( rcode, msg);
- *strchr(msg,':') = NUL; /* V1.1 */
- fpf (stderr,"\n%s", msg);
- }
- }
-
- /*----------Load variable structs **/
-
- for (j=0; j<cdfh->nVars; j++) {
- var = varList[j];
- var->varNum = j;
- rcode = CDFvarInquire( cdfh->id,
- var->varNum,
- &var->name[0],
- &var->dataType,
- &var->stringSize,
- &var->recVariance,
- &var->dimVariances[0]);
- if (rcode < CDF_WARN) { /* V1.2 */
- CDFerror( rcode, msg);
- *strchr(msg,':') = NUL; /* V1.1 */
- fpf (stderr,"\n%s", msg);
- }
-
- #if NSSDC_STANDARD
- /* if there is a FORMAT attribute for this variable;
- get it & put it in format string member.
- */
- attrNum = CDFattrNum( cdfh->id, "FORMAT");
- if ( attrNum >= 0) { /* V1.2 */
- rcode = CDFattrEntryInquire( cdfh->id, attrNum, /* V1.7 */
- var->varNum, &dataType, &nElements);
- if (rcode > CDF_WARN) /* V1.7 */
- switch (dataType) {
- case CDF_CHAR:
- /* Yes FORMAT exists, so get its value for the
- variable from CDF, and save in the var->format.
- */
- rcode = CDFattrGet( cdfh->id, attrNum,
- var->varNum, &var->format[0]);
- if (rcode < CDF_WARN) /* V1.2 */
- strcpy (var->format, "");
- else
- var->format[nElements] = '\0'; /* V1.7 */
- break;
- default:
- strcpy( var->format, "");
- break;
- } /* end switch */
- else { /* V1.7 */
- strcpy (var->format, ""); /* no entry */
- }
- } else {
- strcpy( var->format, ""); /* no FORMAT */
- }
- #else
- strcpy (var->format, ""); /* ignore FORMAT */
- #endif
-
- } /* end for loop thru vars */
-
- /* CDF is open and structs are loaded */
- return ( 1); /* 1 == OK, the cdf is open. */
- } /* end openCDFtoBrowse */
-
-
- void cdfCloseQuit ( )
- {
- CDFstatus rcode;
- long j;
-
- /* Free all the space in the attribute and variable lists
- * and then close the CDF.
- */
- for (j=0; j<cdfh->nAttrs; j++)
- free (attrList[j]);
-
- for (j=0; j<cdfh->nVars; j++)
- free (varList[j]);
-
- rcode = CDFclose ( cdfh->id);
- if(rcode < CDF_OK)printf("Error closing CDF\n");
- } /* end cdfCloseQuit */
-
-
- void catAttrEntry(out, cdfh, attr, entryNum, elemNum )
- char *out;
- struct CDF_DEF *cdfh;
- struct ATTR_DEF *attr;
- long entryNum;
- long elemNum ;
- {
- /*
- This function will concatenate the contents of a
- CDF attribute entry to the string *out.
- The elemNum arg is new. This will display the 0..elemNum-th -1
- element... The exception is when the datatype is _CHAR or _UCHAR,
- then all elements, the whole string, will be concatenated.
- If the elemNum is == FIRST_ELEM, it is a special signal that means
- concat strings in all their elements as "hooey, dewy, and fooey".
- Otherwise only the elem character is concatenated "h", "o", ...
- This functio also does special things for attributes of EPOCH variables.
- */
- CDFstatus rcode;
- static union DATATYPE_UNION value, *valPtr;
- char string[BIG_ENOUGH];
- long item, showStringsByOne;
-
- /* first what dataType ? and how many elements ? */
- rcode = CDFattrEntryInquire( cdfh->id, attr->attrNum, entryNum,
- &attr->dataType, &attr->nElements);
- if (rcode < CDF_WARN) { /* V1.2 */
- CDFerror( rcode, string);
- *strchr(string,':') = NUL; /* V1.1 */
- }
- if ( dataTypeSizeof( attr->dataType) * attr->nElements > BIG_ENOUGH) {
- sprintf(out,
- "<CDFbrowse.catAttrEntry> Too-large attribute. Tell CDFsupport.");
- return;
- }
- rcode = CDFattrGet( cdfh->id,
- attr->attrNum,
- entryNum,
- &value);
-
- /* re-map according to desired element number, and by the funky rules
- for strings. */
- if (elemNum == FIRST_ELEM) { /* we want horizontal strings and
- only 1 element for other types */
- showStringsByOne = FALSE;
- item = 0;
- } else { /* print the nth element, but only length 1 for strings */
- showStringsByOne = TRUE;
- item = dataTypeSizeof( attr->dataType) * elemNum;
- }
- valPtr = (union DATATYPE_UNION *) &value.byted[item];
-
- if (rcode < CDF_WARN) { /* V1.2 */
- CDFerror( rcode, string);
- *strchr(string,':') = NUL; /* V1.1 */
- } else {
- /* Special handling for attributes of an EPOCH variable. */
- if ( entryIsaEpoch( attr->dataType) ) {
- writemTypedValue (string, EPOCH_TYPE, 1, valPtr, "");
- } else { /* not an epoch data */
- if ( showStringsByOne &&
- (attr->dataType == CDF_CHAR ||
- attr->dataType == CDF_UCHAR ) ) {
- writemTypedValue (string, attr->dataType,
- 1, valPtr, "");
- } else {
- writemTypedValue (string, attr->dataType,
- attr->nElements, valPtr, "");
- }
- }
- }
- if (strlen(out) + strlen(string) > (SCREEN_WIDTH-2))
- string[(SCREEN_WIDTH-2) - strlen(out)] = NUL; /* V1.2, V1.3 */
-
- strcat (out, string);
- } /* end catAttrEntry */
-
-
- void writemTitle (out)
- char *out[];
- {
-
- /* write the title if the TITLE attribute exists.*/
- long attrNum = CDFattrNum( cdfh->id, "TITLE "); /* V1.2 */
- if (attrNum < 0) attrNum = CDFattrNum (cdfh->id, "TITLE"); /* V1.2 */
-
- strcpy (out[0], " Title: ");
- if (attrNum < 0)
- strcat (out[0], "--none--");
- else
- catAttrEntry( out[0], cdfh, attrList[attrNum], (long) 0,
- (long) FIRST_ELEM);
- strcat (out[0]," ");
- } /* end writemTitle */
-
-
- void writemCopyright (out)
- char *out[];
- {
- /* write the copyright notice text, ie, CDFdoc() stuff.
- The leading '\n' must be replaced, and all other '\n' are converted to
- '\0' so that WFL likes it.
- */
- int curRow = 0;
- int line = 0;
- int len, k;
- CDFstatus rcode;
- char *p;
-
- rcode = CDFdoc( cdfh->id, &cdfh->version, &cdfh->release,
- &cdfh->docText[0]);
- if (rcode < CDF_WARN) { /* EARLY RETURN !!! */ /* V1.2 */
- strcpy( out[curRow++], " Open the CDF first. ");
-
- /*** CDFerror( rcode, out[curRow++]); ***/ /* V1.1 */
- CDFerror (rcode, errorText); /* V1.1 */
- *strchr(errorText,':') = NUL; /* V1.1 */
- strcpy (out[curRow++], errorText); /* V1.1 */
-
- return;
- }
- len = strlen( cdfh->docText);
- for (k=0; k<len; k++) { /* make acceptable for WFL menus */
- if ( cdfh->docText[k] == '\n') {
- if (k==0)
- cdfh->docText[k] = ' ';
- else {
- cdfh->docText[k] = '\0';
- line++;
- }
- }
- }
- /* write to the out[] */
- p = &cdfh->docText[0];
- for (k=0; k<line; k++) {
- strcpy( out[curRow++], p);
- len = strlen( p);
- p += len +1;
- }
- } /* end writemCopyright */
-
-
- void writemAttrs (out)
- char *out[];
- {
- /*
- This lists definitions of all attributes to the array *out[].
- */
- long j;
- int curRow = 0;
-
- if (cdfh->nAttrs <= 0) { /* RETURN if no attributes */
- spf (out[curRow++],
- " No attributes are defined for this CDF. ");
- return;
- }
- for (j=0; j<cdfh->nAttrs; j++) {
- attr = attrList[j];
- /* prime formatter */
- spf (out[curRow],"%4ld %-16.16s %4ld ",
- j+1, attr->name, attr->maxEntry+1);
- switch( attr->scope) {
- case GLOBAL_SCOPE:
- strcat (out[curRow], "Global ");
- break;
- case VARIABLE_SCOPE:
- strcat (out[curRow], "Variable ");
- break;
- case GLOBAL_SCOPE_ASSUMED:
- strcat (out[curRow], "Assumed Global ");
- break;
- case VARIABLE_SCOPE_ASSUMED:
- strcat (out[curRow], "Assumed Variable");
- break;
- default:
- strcat (out[curRow], "? Unknown scope");
- break;
- }
- curRow++;
- } /* end for j */
- } /* end writemAttrs */
-
-
- void varName (varNum, variable_name)
- long varNum;
- char *variable_name;
- {
- /* What's the name of the numbered variable? */
- if (cdfh->nVars > 0 && varNum >= 0 && varNum < cdfh->nVars)
- strcpy (variable_name, varList[varNum]->name);
- else
- strcpy (variable_name, "?NO_SUCH_VAR?");
- } /* end varName */
-
- void writemAttrEntries (out, attrNum)
- char *out[];
- long attrNum;
- {
- /* nov-1990, heavily modified... -dg.
- This function lists the entries for the attribute to the array out[].
- It only lists the 1st element of each entry, except when dataType ==
- CHAR or UCHAR , then it lists all (in the string array.)
- */
- int curRow = 0;
- CDFstatus rcode;
-
-
-
- long entryNum;
- char eText[CDF_ERRTEXT_LEN+1]; /* V1.1 */
- char tLine[BIG_ENOUGH], tLine2[BIG_ENOUGH];
- long dataType, nElements;
-
- if (cdfh->nAttrs == 0) { /* EARLY RETURN */
- spf (out[curRow++],
- " No attributes are defined for this CDF. ");
- return;
- }
- attr = attrList[attrNum];
- if (attr->maxEntry < 0) { /* EARLY RETURN */
- spf (out[curRow++],
- " No entries are defined. ");
- return;
- }
- for (entryNum=0; entryNum <= attr->maxEntry; entryNum++) {
- sprintf( out[curRow], "%3ld ", entryNum+1);
- rcode = CDFattrEntryInquire( cdfh->id, attr->attrNum,
- entryNum, &dataType, &nElements);
-
- if (rcode >= CDF_OK) { /* V1.2 */
- switch( attr->scope) {
- case GLOBAL_SCOPE:
- case GLOBAL_SCOPE_ASSUMED:
- strcpy( tLine, "{global} ");
- break; /* global case */
-
- case VARIABLE_SCOPE:
- case VARIABLE_SCOPE_ASSUMED:
- varName( entryNum, tLine2 );
- sprintf( tLine, "{%-16s} ", tLine2);
- break; /* variable scope case */
- } /* end switch */
- strcat( tLine, dataTypeName( dataType) );
- strcat( tLine, "[");
- strcat( out[curRow], tLine);
- sprintf( tLine, "%3ld]: ", nElements);
- strcat( out[curRow], tLine);
-
- catAttrEntry( out[curRow], cdfh, attr, entryNum,
- (long) FIRST_ELEM);
- switch (dataType) {
- case CDF_CHAR:
- case CDF_UCHAR:
- break; /* do nothing */
- default:
- if (nElements > 1)
- strcat( out[curRow], " {...}");
- break;
- }
- } else {
- CDFerror( rcode, eText);
- *strchr(eText,':') = NUL; /* V1.1 */
- strcat( out[curRow], eText);
- }
- curRow++;
- } /* end entryNum loop */
-
- } /* end writemAttrEntries */
-
-
- void writemAttrElems (out, attrNum, entryNum)
- char *out[];
- long attrNum;
- long entryNum;
- {
- /* New function for new menu. -dg.
- Writes the elements of an attribute-entry, vertical listing.
- */
- int curRow = 0;
- CDFstatus rcode;
- long dataType, nElements, elem;
-
-
-
- char tLine[BIG_ENOUGH], tLine2[BIG_ENOUGH];
-
- attr = attrList[attrNum];
- rcode = CDFattrEntryInquire( cdfh->id, attr->attrNum,
- entryNum, &dataType, &nElements);
-
- if (rcode < CDF_WARN) { /* EARLY RETURN */ /* V1.2 */
- /*** CDFerror( rcode, out[0]); ***/ /* V1.1 */
- CDFerror (rcode, errorText); /* V1.1 */
- *strchr(errorText,':') = NUL; /* V1.1 */
- strcpy (out[0], errorText); /* V1.1 */
- return;
- }
-
- /* Use the first line to label the box */
- switch( attr->scope) {
- case GLOBAL_SCOPE:
- case GLOBAL_SCOPE_ASSUMED:
- strcpy( tLine, " {global scope} ");
- break; /* global case */
-
- case VARIABLE_SCOPE:
- case VARIABLE_SCOPE_ASSUMED:
- varName( entryNum, tLine2 );
- sprintf( tLine, " {%s} ", tLine2);
- break; /* variable scope case */
- } /* end switch */
- strcat( tLine, dataTypeName( dataType) );
- strcat( tLine, " ");
- strcat( out[curRow++], tLine);
-
- for (elem=0; elem < nElements; elem++) {
- sprintf( tLine, " [%3ld]: ", elem+1);
- strcat( out[curRow], tLine);
- catAttrEntry( out[curRow], cdfh, attr, entryNum, elem);
- curRow++;
- } /* end elem loop */
-
- } /* end writemAttrElems */
-
-
-
- void writemVars( out)
- char *out[];
- {
- /*
- This lists definitions of all variables to the array out[][].
- */
- long j, k;
- int curRow = 0;
- char appender[BIG_ENOUGH];
- if (cdfh->nVars == 0) { /* RETURN if no variables */
- spf (out[curRow++],
- " No variables are defined for this CDF. ");
- return;
- }
-
- for (j=0; j<cdfh->nVars; j++) {
- var = varList[j];
- /* prime formatter */
- spf (out[curRow],"%3ld %-16s %s", var->varNum+1, var->name,
- dataTypeName( var->dataType) );
- switch( var->dataType) {
- case CDF_UCHAR:
- case CDF_CHAR:
- /* prime formatter, strings */
- spf (appender, "[%3ld] ", var->stringSize);
- break;
- default:
- /* prime formatter, non-char */
- spf (appender, " ");
- break;
- }
- /* prime formatter, danglers */
- strcat( out[curRow], appender);
- varncTF (out[curRow], var->recVariance, 5);
- strcat (out[curRow]," [");
- for (k=0; k<cdfh->nDims; k++) {
- varncTF (out[curRow], var->dimVariances[k], 1);
- if ( k+1 != cdfh->nDims) strcat (out[curRow],",");
- }
- strcat (out[curRow],"]");
-
- for (k=0; k< (10 -(cdfh->nDims-1)); k++)
- strcat (out[curRow]," ");
- if (cdfh->nDims > 0)
- strcat (out[curRow++]," ");
- else
- strcat (out[curRow++]," ");
- } /* end j loop */
- } /* end writemVars */
-
-
- void writemVarDef (out, varNum)
- char *out[];
- long varNum;
- {
- /*
- This function lists the variable-scope attributes of a variable to
- the array *out[].
- */
- int curRow = 0;
- CDFstatus rcode;
- long j, entryNum, attrNum, dataType, nElements;
- long num_var_attributes;
- char eText[CDF_ERRTEXT_LEN+1], /* V1.1 */
- string[BIG_ENOUGH];
-
- if (cdfh->nVars < 1) { /* RETURN if no variables */
- spf (out[curRow++],
- " No variables are defined for this CDF. ");
- return;
- }
-
- if (cdfh->nAttrs < 1) { /* EARLY RETURN */
- spf (out[curRow++],
- " No attributes are defined for this CDF. ");
- return;
- }
-
- /* count non-global attributes. */
- num_var_attributes = 0;
- for (attrNum=0; attrNum < cdfh->nAttrs; attrNum++)
- if ( attrList[attrNum]->scope == VARIABLE_SCOPE ||
- attrList[attrNum]->scope == VARIABLE_SCOPE_ASSUMED)
- num_var_attributes++;
-
- /* EARLY RETURN!! */
- if (num_var_attributes == 0) {
- spf (out[curRow++],
- " No VARIABLE_SCOPE attributes apply to this variable. ");
- return;
- }
-
- /* Get attribs for this variable */
- var = varList[varNum];
- entryNum = var->varNum;
-
- for (j=0, attrNum=0; attrNum < cdfh->nAttrs; attrNum++) {
- attr = attrList[attrNum];
- switch( attr->scope ) {
- case VARIABLE_SCOPE:
- case VARIABLE_SCOPE_ASSUMED:
- j++;
- spf (out[curRow], " %2ld {%-16.16s} ", j, attr->name);
-
- rcode = CDFattrEntryInquire( cdfh->id, attr->attrNum,
- entryNum, &dataType, &nElements);
- if (rcode < CDF_WARN) { /* V1.2 */
- CDFerror( rcode, eText);
- *strchr(eText,':') = NUL; /* V1.1 */
- strcat( out[curRow], eText);
- } else {
- strcat( out[curRow], dataTypeName( dataType) );
- sprintf( string, "[%3ld]: ", nElements);
- strcat( out[curRow], string);
- catAttrEntry (out[curRow], cdfh,
- attr, entryNum, (long) FIRST_ELEM);
- switch (dataType) {
- case CDF_CHAR:
- case CDF_UCHAR:
- break; /* do nothing */
- default:
- if (nElements > 1)
- strcat( out[curRow], " {...}");
- break;
- }
- }
- curRow++;
- break;
- default: /* not variable-scope, skip it */
- break;
- }
- }
- } /* end writemVarDef */
-
-
- long productLowerDims( currentDim, dimSizes)
- long currentDim;
- long dimSizes[];
- {
- /* got guts of this from toolkit.
- Algorithm essentially the same.
- Nov-1990, dg.
- */
- long k, product = 1;
- /* compute product of all dimension sizes less than
- currentDim-th.
- */
- for (k=0; k<currentDim; k++) {
- if (dimSizes[k] > 0)
- product *= dimSizes[k];
- }
- return product;
- } /* end productLowerDims */
-
- long productHigherDims( currentDim, dimSizes)
- long currentDim;
- long dimSizes[];
- {
- /* new function to work with row-major indexing.
- Nov-1990, dg.
- */
- long k, product = 1;
- /* compute product of all dimension sizes HIGHER than currentDim.
- */
- for (k=currentDim+1; k<cdfh->nDims; k++) {
- if (dimSizes[k] > 0)
- product *= dimSizes[k];
- }
- return product;
- } /* end productHigherDims */
-
- void indexVecColMajor( currentItem, cdfh, iVector, var )
- long currentItem;
- struct CDF_DEF *cdfh;
- long iVector[];
- struct VAR_DEF *var ;
- {
- /* got guts of this from toolkit, previous name computeIndexVector.
- Algorithm is altered to return 0-based indexes.
- Nov-1990, dg.
- */
- /*
- * Convert the currentItem to the index vector for a CDF variable.
- * The scheme is COLUMN-MAJOR -- Leftmost index varies the fastest.
- */
- long j, next, index, product;
- long temp_dimSizes[CDF_MAX_DIMS];
- /*
- Set the temp dimSizes vector by setting the size of
- a dimension to 1 whenever the variable has no variance
- in that dimension.
- */
- for (j=0; j < cdfh->nDims; j++) {
- /* Zero-initialize the vector */
- iVector[j] = 0;
- if (var->dimVariances[j])
- temp_dimSizes[j] = cdfh->dimSizes[j];
- else
- temp_dimSizes[j] = 1;
- }
-
- /* Compute the index vector, ZERO-base address, column major.
- */
- next = currentItem;
- for (j=cdfh->nDims; j>=0; j--) {
- product = productLowerDims (j, temp_dimSizes);
- index = (long) (next / product);
- next = (next % product);
- iVector[j] = index;
- }
- } /* end indexVecColMajor */
-
-
- void indexVecRowMajor( currentItem, cdfh, iVector, var )
- long currentItem;
- struct CDF_DEF *cdfh;
- long iVector[];
- struct VAR_DEF *var;
- {
- /* New function based on indexVecColMajor, but indexes Row-Major.
- Algorithm returns 0-based indexes.
- Nov-1990, dg.
- */
- /*
- * Convert the currentItem to the index vector for a CDF variable.
- * The scheme is ROW-MAJOR -- Rightmost index varies the fastest.
- */
- long j, next, index, product;
- long temp_dimSizes[CDF_MAX_DIMS];
- /*
- Set the temp dimSizes vector by setting the size of
- a dimension to 1 whenever the variable has no variance
- in that dimension.
- */
- for (j=0; j < cdfh->nDims; j++) {
- /* Zero-initialize the vector */
- iVector[j] = 0;
- if (var->dimVariances[j])
- temp_dimSizes[j] = cdfh->dimSizes[j];
- else
- temp_dimSizes[j] = 1;
- }
-
- /* Compute the index vector, ZERO-base address, row major.
- */
- next = currentItem;
- for (j=0; j<cdfh->nDims; j++) {
- product = productHigherDims (j, temp_dimSizes);
- index = (long) (next / product);
- next = (next % product);
- iVector[j] = index;
- }
- } /* end indexVecRowMajor */
-
-
- int writemVarVals( out, varNum, record, maxLines)
- char *out[];
- long varNum;
- long record;
- long maxLines;
- {
- /* This function lists the values of the Variable for the record to
- the array *out[]. However, no more than maxLines will be listed.
- Record comes in 0-based.
- */
- int curRow = 0;
- CDFstatus rcode;
- long j;
- long iVector[CDF_MAX_DIMS+1];
- union DATATYPE_UNION value;
-
- char string[BIG_ENOUGH];
- long item, numItems;
- if (cdfh->nVars == 0) { /* RETURN if no variables */
- spf (out[curRow++],
- " No variables are defined for this CDF. ");
- return (-1);
- }
- /* RETURN if bad record Number */
- if (record < 0 || record > cdfh->maxRec) {
- spf (out[0], " Record %ld -- Out of Bounds (%ld records in this CDF) ",
- record+1, cdfh->maxRec+1);
- return (-1);
- }
-
- var = varList[varNum];
- numItems = 1;
- curRow = 0;
-
- /* Compute number of items according to variance vector */
- for (j=0; j<cdfh->nDims; j++) {
- if (var->dimVariances[j])
- numItems *= cdfh->dimSizes[j];
- }
-
- if (var->recVariance) {
- spf (out[curRow++], " %ld items. Record variance is TRUE. ",
- numItems);
- } else {
- spf (out[curRow++], " %ld items. Record variance is FALSE. ",
- numItems);
- spf (out[curRow++], " These values constant for all records. ");
- }
-
- for (item=0; item<numItems; item++) {
- if (!var->recVariance)
- spf (out[curRow], " (%3ld) *:[", item +1);
- else
- spf (out[curRow], " (%3ld) %ld:[", item +1, record +1);
-
- /* optimize the vector for getting the same way the data is
- stored, either row- or col-major */
- if (cdfh->majority == COL_MAJOR)
- indexVecColMajor( item, cdfh, iVector, var);
- else
- indexVecRowMajor( item, cdfh, iVector, var);
-
- for (j=0; j<cdfh->nDims; j++) {
- /* if the variance in this dim is false display
- wildcard index number.
- */
- if ( (iVector[j] == 0) && (!var->dimVariances[j]) )
- spf (string, " *");
- else
- spf (string, "%3ld", iVector[j] +1);
-
- strcat( out[curRow], string);
- if (j+1 < cdfh->nDims)
- strcat( out[curRow], ",");
- }
- strcat( out[curRow], "]= ");
-
- rcode = CDFvarGet( cdfh->id, varNum, record, iVector, &value);
-
- if( rcode < CDF_WARN) { /* V1.2 */
- CDFerror( rcode, string);
- *strchr(string,':') = NUL; /* V1.1 */
- } else {
- if ( varIsaEpoch( var->dataType) ) {
- writemTypedValue (string, EPOCH_TYPE,
- var->stringSize, &value, var->format);
- } else { /* not an epoch */
- writemTypedValue (string, var->dataType,
- var->stringSize, &value, var->format);
- }
- }
-
- if (strlen(out[curRow]) + strlen(string) > (SCREEN_WIDTH-2))
- string[(SCREEN_WIDTH-2) - strlen(out[curRow])] = NUL; /* V1.3 */
-
- strcat( out[curRow], string);
-
- if (curRow == maxLines-3) { /* whoops */
- spf (string, "Listing truncated at %ld lines.", maxLines-3);
- /* place warning at end of output only, V1.8 */
- strcat (out[curRow+1], string);
- item = numItems+1; /* break the loop */
- }
- curRow++;
- } /* end item loop */
- return (0); /* means OK */
-
- } /* end writemVarVals */
-
- /*******************/
-
-
- long numVars ()
- {
- /* How many variables in the current CDF ? */
- return (cdfh->nVars);
- } /* end numVars */
-
-
- long numAttrs ()
- {
- /* How many attributes in the current CDF ? */
- return (cdfh->nAttrs);
- } /* end numAttrs */
-
- long nthVarAttr ( n)
- long n;
- {
- /* new function, nov-1990, dg. */
- /* given n (0-based), return the CDF's entry-index to the nth
- variable-scoped attribute.
- */
- long k, nth = -1;
- for (k=0; k<cdfh->nAttrs; k++) {
- switch (attrList[k]->scope) {
- case VARIABLE_SCOPE:
- case VARIABLE_SCOPE_ASSUMED:
- nth++;
- if (nth == n)
- return k;
- break;
- default:
- break;
- }
- }
- return -1; /* -1 mean no var-scope items */
- } /* end nthVarAttr */
-
-
- long maxAttrEntry( attrNum)
- long attrNum;
- {
- /* nov-1990, new function. -dg. */
- /* what's the maximum entry for this attribute? */
- if (numAttrs() > 0)
- return (attrList[attrNum]->maxEntry);
- else
- return -1;
- } /* end maxAttrEntry */
-
-
- long numElemsAttr( attrNum, entryNum)
- long attrNum;
- long entryNum;
- {
- /* nov-1990, new function. -dg. */
- /* How many elements in this attribute, this entry? */
- CDFstatus rcode;
- long dataType, nElements;
- rcode = CDFattrEntryInquire( cdfh->id, attrNum, entryNum,
- &dataType, &nElements);
- if (rcode >= CDF_OK)
- return nElements;
- else
- return (0);
- } /* end numElemsAttr */
-
-
- void attribName (attrNum, attrName)
- long attrNum;
- char *attrName;
- {
- /* What's the name of the numbered attribute? */
- if (cdfh->nAttrs > 0 && attrNum >= 0 && attrNum < cdfh->nAttrs)
- strcpy (attrName, attrList[attrNum]->name);
- else
- strcpy (attrName, "?NO_SUCH_ATTR?");
- return;
- } /* end attribName */
-
-
-
-
- Boolean varIsaEpoch( dataType)
- long dataType;
- {
- /* Nov-1990, new function,
- return TRUE iff this is an EPOCH
- */
- switch (dataType) {
- case CDF_EPOCH:
- return TRUE; /* ...V1.7 */
-
- default:
- break;
- }
- return FALSE;
- } /* end varIsaEpoch */
-
-
- Boolean entryIsaEpoch(dataType)
- long dataType;
- {
- /* Nov-1990, new function,
- return TRUE iff this Attribute Entry is an EPOCH
- */
- switch (dataType) { /* V1.7 */
- case CDF_EPOCH:
- return TRUE; /* ...V1.7 */
-
- default: /* no such thing as a Global-scope EPOCH. would be
- nice however */ /* says who?, V1.7 */
- break;
- }
- return FALSE;
- } /* end entryIsaEpoch */
-
-
- void writemTypedValue(out, dataType, numBytes, valuePtr, passedfmt)
- char *out;
- long dataType;
- long numBytes;
- void *valuePtr;
- char passedfmt[]; /* will be "" if FORMAT attribute/entry not present
- or is to be ignored */
- {
- /* revised, inproved for version 2 types from putValueMemory().
- Formats a CDF value according to CDF dataType and numBytes,
- writes the string version to out[].
- passedfmt is a FORTRAN format string, or "" for defaults.
- The complication is translating the Fortran to C formatters.
- Note special non-CDF type, EPOCH_TYPE for readable epoch values.
- */
- long formatType = 'D'; /* NOT for decimal; for default in each type */
- char format[32];/* = "%";*/
- char fmtTMP[32];/* = "";*/
- char fmt[32];/* = "";*/
- long j, k=0;
- union DATATYPE_UNION *dtu;
-
- strcpy(format,"%");
- fmtTMP[0] = '\0';
- fmt[0] = '\0';
- strcpy( fmt, passedfmt);
- for (k=0; k<strlen(fmt); k++)
- if (fmt[k] >= 'a' && fmt[k] <= 'z')
- fmt[k] = toupper(fmt[k]);
-
- if ( strchr(fmt, 'E') != NULL) /* V1.4 */
- formatType = 'E';
-
- if ( strchr(fmt, 'F') != NULL) /* V1.4 */
- formatType = 'F';
-
- if ( strchr(fmt, 'G') != NULL) /* V1.4 */
- formatType = 'G';
-
- for (j=0, k=0; j<strlen( fmt); j++) {
- if ( (isdigit(fmt[j])) || (fmt[j]=='.') ||
- (fmt[j] == '+') || (fmt[j]=='-') ) {
- fmtTMP[k] = fmt[j];
- k++;
- }
- }
- fmtTMP[k] = '\0';
-
- strcat (format, fmtTMP);
- /* the format string now has "%" or "%8.2", etc. prefixes
- for the d,f,s, etc... printf formatters.
- */
- dtu = (union DATATYPE_UNION *) valuePtr;
-
- switch (dataType) {
- case CDF_BYTE:
- case CDF_INT1: /* always d format */
- strcat (format, "d");
- sprintf (out, format, dtu->byted[0]);
- break;
- case CDF_UINT1:
- strcat (format, "u");
- sprintf (out, format, dtu->uchard[0]);
- break;
- case CDF_INT2: /* always d format */
- strcat (format, "d");
- sprintf (out, format, dtu->int2);
- break;
- case CDF_UINT2: /* always u format */
- strcat (format, "u");
- sprintf (out, format, dtu->uint2);
- break;
- case CDF_INT4: /* always ld format */
- strcat (format, "ld");
- sprintf (out, format, dtu->int4);
- break;
- case CDF_UINT4: /* always lu format */
- strcat (format, "lu");
- sprintf (out, format, dtu->uint4);
- break;
- case CDF_FLOAT:
- case CDF_REAL4: /* three formats */
- switch ( formatType) {
- case 'E':
- strcat (format, "E");
- break;
- case 'F':
- strcat (format, "f");
- break;
- default:
- /* strcat (format, "G"); */
- if (dtu->real4 < -1000000000.0 ||
- dtu->real4 > 1000000000.0)
- strcat (format, "E");
- else
- strcat (format, "f");
- break;
- }
- sprintf (out, format, dtu->real4);
- break;
- case CDF_DOUBLE:
- case CDF_REAL8:
- switch ( formatType) {
- case 'E':
- strcat (format, "E");
- break;
- case 'F':
- strcat (format, "f");
- break;
- default:
- /* strcat (format, "G"); */
- if (dtu->real8 < -1000000000.0 ||
- dtu->real8 > 1000000000.0)
- strcat (format, "E");
- else
- strcat (format, "f");
- break;
- }
- sprintf (out, format, dtu->real8);
- break;
- case CDF_CHAR:
- if (numBytes > SCREEN_WIDTH)
- sprintf (format, "\"%%.%ds\"",
- SCREEN_WIDTH);
- else
- sprintf (format, "\"%%.%lds\"", numBytes);
- sprintf (out, format, dtu->byted);
- break;
- case CDF_UCHAR:
- if (numBytes > SCREEN_WIDTH)
- sprintf (format, "\"%%.%ds\"",
- SCREEN_WIDTH);
- else
- sprintf (format, "\"%%.%ds\"", numBytes);
- sprintf (out, format, dtu->uchard);
- break;
- case EPOCH_TYPE: /* not a true CDF built-in type --- a possible
- conflict will occur if a CDF type has the same
- value. However, the compiler should not allow
- duplicate case values. At least the VAX does
- not. -- dg.
- */
- { struct EPOCH epoch;
- epoch.tSince0 = dtu->real8;
- sprintf( out, "%s", epochString( &epoch));
- }
- break;
- default:
- sprintf (out, "<writemTypedValue> BAD dataType = %s\n",
- dataTypeName( dataType) );
- break;
- } /* end switch */
-
- } /* end writemTypedValue */
-
-
-